********************************************************************************
** 	TITLE:		CA15_results                                                  ** 	
**	AUTHOR:	    Review version                                                **
**	DATA:       table_tableau12_2015                                          **
**	VERSION:	Stata 16					                                  **
**	DATE:		October 2022  				                                  **
********************************************************************************

* Version control

version 16.0

* Import the dataset

copy https://www.elections.ca/res/rep/off/ovr2015app/41/data_donnees/table_tableau12.csv table_tableau12_2015.csv, replace
type table_tableau12_2015.csv
import delimited table_tableau12_2015.csv, clear

* Rename variables

rename ïprovince iprovince 
rename electoraldistrictnamenomdecircon districtname15
rename electoraldistrictnumbernumãrodec districtnumber15
rename candidatecandidat candidate15
rename candidateresidencerãsidenceducan residence15
rename candidateoccupationprofessionduc occupation15
rename votesobtainedvotesobtenus numberofvotes15
rename percentageofvotesobtainedpourcen numberofvotespercent15
rename majoritymajoritã majoritynumber15
rename majoritypercentagepourcentagedem majoritypercent15

* Generate dichotomous variable for each party 

gen lib15 = strpos(candidate15, "Liberal/LibÃ©ral") > 0
gen con15 = strpos(candidate15, "Conservative/Conservateur") > 0
gen ndp15 = strpos(candidate15, "NDP-New Democratic Party/NPD-Nouveau Parti dÃ©mocratique") > 0
gen green15 = strpos(candidate15, "Green Party/Parti Vert") > 0
gen bq15 = strpos(candidate15, "Bloc QuÃ©bÃ©cois/Bloc QuÃ©bÃ©cois") > 0

* Generate party variable

gen party15 = 1 if con15 == 1
replace party15 = 2 if ndp15 == 1
replace party15 = 3 if lib15 == 1
replace party15 = 4 if bq15 == 1
replace party15 = 5 if green15 == 1
replace party15 = 88 if party15 == . 

* Generate rank of parties

gsort districtname15 -numberofvotes15
bysort districtname15 : gen rank15 = _n

gen first15 = party15 if rank15 == 1
gen second15 = party15 if rank15 == 2
gen third15 = party15 if rank15 == 3

bysort districtname15 : gen winner15 = first15[1]
bysort districtname15 : gen runnerup15 = second15[2]
bysort districtname15 : gen thirdplace15 = third15[3]

* Generate margins of victory

bysort districtname15 : gen margin15 = numberofvotespercent15[1] - numberofvotespercent15[2]

* Generate effective number of electoral parties

gen sqrvotepr15 = (numberofvotespercent15/100)^2

bysort districtname15 : egen sumsqrvotepr15 = sum(sqrvotepr15)

gen enep15 = 1/sumsqrvotepr15

* Labelling parties

label define party 1 "Conservative Party" 2 "New Democratic Party" 3 "Liberal Party" 4 "Bloc Quebecois" 5 "Green Party" 88 "Other"
label values winner15 runnerup15 thirdplace15 party

* Selecting, ordering, and sorting variables

keep districtnumber15 districtname15 party15 rank15 numberofvotes15 numberofvotespercent15 margin15 enep15 winner15 runnerup15 thirdplace15

order districtnumber15 districtname15 party15 rank15 numberofvotes15 numberofvotespercent15 margin15 enep15 winner15 runnerup15 thirdplace15

sort districtnumber15 rank15

drop if rank15 > 3

* Drop duplicates

duplicates tag districtnumber15, gen(dup)
duplicates drop districtnumber15, force
drop dup

* Create election variable

gen election15 = "2015 Canadian federal election"

* Saving file

save CA15_results, replace